package com.jwm.power.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.jwm.power.model.common.EnumMenu;
import com.jwm.power.model.common.EnumOperate;
import com.jwm.power.model.common.EnumResult;
import com.jwm.power.model.entity.Depts;
import com.jwm.power.model.entity.Logs;
import com.jwm.power.model.entity.Menus;
import com.jwm.power.model.entity.Users;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.repository.LogsRepository;
import com.jwm.power.repository.DeptsRepository;
import com.jwm.power.service.DeptsService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestDataBinder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional
public class DeptsServiceImpl implements DeptsService {
    @Autowired
    private DeptsRepository deptsRepository;
    @Autowired
    private LogsRepository logsRepository;

    //  设置部门
    @Override
    public ResponseData SaveDepts(HttpServletRequest request) {
        try {
            Users users = (Users) StpUtil.getSession().get("users");
            Depts model_New = new Depts();
            ServletRequestDataBinder binder =
                    new ServletRequestDataBinder(model_New);
            binder.bind(request);
            if (model_New.getParentcode() == null || model_New.getParentcode().equals("")) {
                model_New.setParentcode("00");
            }
            Integer operationType = 0;
            if (model_New.getDeptid() != null && model_New.getDeptid() > 0) {
                operationType = 1;
                //  检查部门名称是否存在
                List<Depts> deptsList = deptsRepository.findByDeptnameAndDeptidNot(model_New.getDeptname(), model_New.getDeptid());
                if (deptsList.size() > 0) {
                    return new ResponseData(EnumResult.DEPT_FAIL_NAME, null);
                }
            } else {
                String maxCode = deptsRepository.getMaxCode(model_New.getParentcode());
                maxCode = maxCode != null ? maxCode : "0";
                String parentCode = model_New.getParentcode().equals("00") ? "" : model_New.getParentcode();
                model_New.setDeptcode(parentCode + String.format("%02d", Integer.parseInt(maxCode) + 1));
                Integer sn = deptsRepository.getMaxSn(model_New.getParentcode());
                model_New.setSn(sn + 1);
                //  检查部门名称是否存在
                List<Depts> deptsList = deptsRepository.findByDeptnameAndDeptidNot(model_New.getDeptname(), 0);
                if (deptsList.size() > 0) {
                    return new ResponseData(EnumResult.DEPT_FAIL_NAME, null);
                }
            }

            deptsRepository.save(model_New);

            //  添加日志
            Logs log = Ut.SaveLog("depts", model_New.getDeptname(), operationType == 1 ? EnumOperate.EDIT : EnumOperate.ADD, users);
            logsRepository.save(log);
            return new ResponseData(EnumResult.SUCCESS, model_New);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
    }
}
